import redis from '../utils/redis.js'

class RoomService {
    static async roomCreate(params) {
        const { device_id, room_id } = params
        const DEVICE_PREFIX = 'webrtc:device:'

        try {
            await redis.sadd('client_device', device_id)

            // 先清除 device_id 在其他房间的绑定
            const deviceKeys = await redis.keys(DEVICE_PREFIX + '*')
            for (const key of deviceKeys) {
                if (key !== DEVICE_PREFIX + room_id) {
                    await redis.srem(key, device_id)
                }
            }

            await redis.sadd(DEVICE_PREFIX + room_id, device_id)
        } catch (error) {
            throw new global.errs.ServerError('房间创建失败', 10004)
        }

        return room_id
    }
}

export default RoomService
